package com.neuedu.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.entity.Questions;
import com.neuedu.mapper.QuestionsMapper;
import com.neuedu.service.Assistant;
import com.neuedu.service.QuestionsService;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.service.AiServices;
import jakarta.annotation.Resource;
import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.fdf.FDFDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class QuestionsServiceImpl extends ServiceImpl<QuestionsMapper, Questions> implements QuestionsService {

    @Override
    public boolean add(Questions questions) {
        return this.save(questions);
    }

    @Override
    public boolean delete(long  id) {
        return this.removeById(id);
    }

    @Override
    public boolean update(Questions questions) {
        return this.updateById(questions);
    }

    @Override
    @Transactional
    public boolean upload(String content) {
        List<Questions> questions = JSON.parseArray(content, Questions.class);
        List<Questions> list = new ArrayList<>();
        for (Questions question : questions) {
            if (check(question.getQuestionType().name(), question.getQuestionText())) {
                list.add(question);
            }
        }
        return this.saveBatch(list);
    }
    private boolean check(String type, String text) {
        QueryWrapper<Questions> wrapper = new QueryWrapper<>();
        wrapper.eq("question_type", type)
                .eq("question_text", text);
        return this.count(wrapper) == 0;
    }
    @Override
    public List<Questions> practice(Integer count) {
        return this.getBaseMapper().practice(count);
    }
}
